#include "LeH/main.h"

//1.图书信息录入
void InsertBooks01(LeBookLink BookHead, LeStuLink StuHead) {
    int Flag = 1;//是否继续输入标识
    int BookID = 0;//书籍ID, 出版号
    long BPID = 0;
    char BName[100], BAName[100];//书籍名称, 作者姓名
    int BState = 1;//书籍状态 0被借出 1可借出 -1损坏等不可用状态
    int SID = 0;//借出人ID

    //获取当前ID最大值
    BookID = LeGetMaxBookID(BookHead);
    while (Flag != 0) {

        BookID++;//书籍ID+1
        printf("书籍ID:%d\n", BookID);
        printf("请输入出版号(出版号允许重复录入):");
        scanf("%ld", &BPID);
        //判断出版号是否已存在，存在则自动读入书名和作者名
        if (LeGetBPIDExist(BookHead, BPID)) {
            printf("该出版号已存在，正在自动获取\n");
            LeGetBNameANDBAName(BookHead, BPID, BName, BAName);
            printf("获取完毕:\n书籍名称:%s\n作者姓名:%s\n", BName, BAName);
        } else {
            printf("请输入书籍名称:");
            scanf("%s", BName);
            printf("请输入作者姓名:");
            scanf("%s", BAName);
        }
        //录入到链表
        LeInsertBook(BookHead, StuHead, BookID, BPID, BName, BAName, BState, SID);
        printf("录入成功！继续录入输入1，结束录入输入0:");
        scanf("%d", &Flag);
    }
}

//2.图书信息查询
void SelectBooks02(LeBookLink BookHead) {
    //打印菜单选择功能
    printf("\t图书查询菜单\n"
           "\t1.书籍序号查询\n"
           "\t2.书籍名称查询\n"
           "\t3.书籍作者查询\n"
           "\t4.书籍书号查询\n"
           "\t5.书库书籍数量\n"
           "\t6.可借图书数量\n"
           "\t7.借出图书数量\n"
           "\t8.某人所借书籍\n"
           "\t0.返回上一级\n"
           "请输入对应数字进入功能:");

    int CDGN;
    scanf("%d:", &CDGN);//选择菜单功能

    int BID = 0, SID = 0;
    long BPID = 0;
    char BName[100], BAName[100];

    switch (CDGN) {
        case 0:
            CDGN = 0;
            return;
        case 1:
            printf("请输入要查询的书籍ID：");
            scanf("%d", &BID);
            LeGetBookInfoFroBID(BookHead, BID);
            break;
        case 2:
            printf("请输入要查询的书籍名称：");
            scanf("%s", BName);
            LeGetBookInfoFroBName(BookHead, BName);
            break;
        case 3:
            printf("请输入要查询的作者名称：");
            scanf("%s", BAName);
            LeGetBookInfoFroBAName(BookHead, BAName);
            break;
        case 4:
            printf("请输入要查询的书籍出版号：");
            scanf("%ld", &BPID);
            LeGetBookInfoFroBPID(BookHead, BPID);
            break;
        case 5:
            LeGetBookCount(BookHead, 2);
            break;
        case 6:
            LeGetBookCount(BookHead, 1);
            break;
        case 7:
            LeGetBookCount(BookHead, 0);
            break;
        case 8:
            printf("请输入要查询的借书人ID：");
            scanf("%d", &SID);
            LeGetBookCountFroSID(BookHead, SID);
            break;
        default:
            return;
    }
}

//3.书籍下架删除
void DelBooks03(LeBookLink BookHead, LeStuLink StuHead) {
    //打印菜单选择功能
    printf("\t图书删除菜单\n"
           "\t1.书籍序号删除\n"
           "\t2.书籍名称删除\n"
           "\t3.书籍作者删除\n"
           "\t4.书籍书号删除\n"
           "\t0.返回上一级\n"
           "请输入对应数字进入功能:");

    int CDGN = 0;
    int BID = 0;
    long BPID = 0;
    char BName[100], BAName[100];
    scanf("%d:", &CDGN);//选择菜单功能
    switch (CDGN) {
        case 0:
            break;
        case 1:
            printf("请输入要删除的书籍序号：");
            scanf("%d", &BID);
            LeDelBookFroBID(BookHead, StuHead, BID);
            break;
        case 2:
            printf("请输入要删除的书籍名称：");
            scanf("%s", BName);
            LeDelBookFroBName(BookHead, StuHead, BName);
            break;
        case 3:
            printf("请输入要删除的作者名称：");
            scanf("%s", BAName);
            LeDelBookFroBAName(BookHead, StuHead, BAName);
            break;
        case 4:
            printf("请输入要删除的出版号：");
            scanf("%ld", &BPID);
            LeDelBookFroBPID(BookHead, StuHead, BPID);
            break;
        default:
            return;
    }
}

//4.学生注册借书
void StuRegisterBooks04(LeBookLink BookHead, LeStuLink StuHead) {
    int StuID = LeGetMaxStuID(StuHead) + 1;
    char SName[20];
    int SQuota = 0;
    int SState = 1;
    printf("请输入借书人姓名:");
    scanf("%s", SName);
    printf("请输入可借书额度:");
    scanf("%d", &SQuota);
    LeInsertStu(BookHead, StuHead, StuID, SName, SQuota, SState);
    printf("注册完成\n借书人ID：%d\n借书人：%s\n可用借书额度：%d\n", StuID, SName, SQuota);
}

//5.办理借书手续
void StuBorrowBooks05(LeBookLink BookHead, LeStuLink StuHead) {

    int SID = 0, BID = 0, EFlag = 1;//借书人ID， 书籍ID， 异常标志
    int SQuota = 0, SState = 0;//借书人余额和状态
    char SName[100] = "借书人昵称";

    printf("请输入借书人ID：");
    scanf("%d", &SID);
    while (EFlag) {
        //判断用户状态以及余额是否充足
        EFlag = LeGetStuNameAndQuotaAndState(StuHead, SID, SName, &SQuota, &SState);
        if (EFlag == -1 || SState == -1) {
            printf("用户不存在或已注销！\n");
            StuBorrowBooks05(BookHead, StuHead);
            return;
        }
        if (SState == 0) {
            printf("用户已挂失！\n");
            return;
        }
        if (SQuota < 1) {
            printf("用户余额不足！\n余额：%d\n", SQuota);
            return;
        }
        printf("用户昵称：%s\n", SName);
        printf("请输入书籍ID：");
        scanf("%d", &BID);

        LeBorrowBook(BookHead, StuHead, BID, SID);
        printf("继续借书输入1，返回上一级输入0：");
        scanf("%d", &EFlag);
    }

}

//6.办理还书手续
void StuReturnBooks06(LeBookLink BookHead, LeStuLink StuHead) {
    int SID = 0, BID = 0, Flag = 1, E = 0;
    printf("请输入还书人ID：");
    scanf("%d", &SID);
    while (Flag){
        printf("请输入书籍ID：");
        scanf("%d", &BID);
        printf("书籍是否正常，正常请输入1，破损等不可用输入0：");
        scanf("%d", &E);
        LeReturnBook(BookHead, StuHead, BID, E);
        printf("继续还书输入1，结束输入0：");
        scanf("%d", &Flag);
    }

}

//7.学生状态更改
void StuState07(LeBookLink BookHead, LeStuLink StuHead) {
    //打印菜单选择功能
    printf("\t图书查询菜单\n"
           "\t1.增加可用额度\n"
           "\t2.减少可用额度\n"
           "\t3.挂失\n"
           "\t4.注销\n"
           "\t5.取消挂失/注销\n"
           "\t6.完全删除用户\n"
           "\t0.返回上一级\n"
           "请输入对应数字进入功能:");

    int CDGN;
    scanf("%d:", &CDGN);//选择菜单功能

    int SID = 0, AddN = 0, SubN = 0;

    switch (CDGN) {
        case 0:
            break;
        case 1:
            printf("请输入需要增加额度的用户ID：");
            scanf("%d", &SID);
            printf("请输入增加的额度：");
            scanf("%d", &AddN);
            LeAddStuQuota(BookHead, StuHead, SID, AddN);
            break;

        case 2:
            printf("请输入需要减少额度的用户ID：");
            scanf("%d", &SID);
            printf("请输入减少的额度：");
            scanf("%d", &SubN);
            LeSubStuQuota(BookHead, StuHead, SID, SubN);
            break;

        case 3:
            printf("请输入需要挂失的用户ID：");
            scanf("%d", &SID);
            LeSetStuState(BookHead, StuHead, SID, 0);
            break;
        case 4:
            printf("请输入需要注销的用户ID：");
            scanf("%d", &SID);
            LeSetStuState(BookHead, StuHead, SID, -1);
            break;
        case 5:
            printf("请输入需要取消挂失/注销的用户ID：");
            scanf("%d", &SID);
            LeSetStuState(BookHead, StuHead, SID, 1);
            break;
        case 6:
            printf("请输入需要完全删除的用户ID：");
            scanf("%d", &SID);
            LeDelStu(BookHead, StuHead, SID);
            break;
        default:
            return;
    }
}

int main() {
    //从文件读入到结构体
    LeBookLink BookHead = LeGetBookLinkFroFile();
    LeStuLink StuHead = LeGetStuLinkFroFile();

    int Flag = 1;//标志程序运行
    while (Flag != 0) {//不为0则重复运行不会停止

        //打印菜单选择功能
        printf("\t菜单\n"
               "\t1.图书信息录入\n"
               "\t2.图书信息查询\n"
               "\t3.书籍下架删除\n"
               "\t4.学生注册借书\n"
               "\t5.办理借书手续\n"
               "\t6.办理还书手续\n"
               "\t7.学生状态更改\n"
               "\t0.结束程序\n"
               "请输入对应数字进入功能:");

        int CDGN;
        scanf("%d:", &CDGN);//选择菜单功能
        switch (CDGN) {
            case 0:
                Flag = 0;
                continue;
            case 1:
                InsertBooks01(BookHead, StuHead);
                continue;
            case 2:
                SelectBooks02(BookHead);
                continue;
            case 3:
                DelBooks03(BookHead, StuHead);
                continue;
            case 4:
                StuRegisterBooks04(BookHead, StuHead);
                continue;
            case 5:
                StuBorrowBooks05(BookHead, StuHead);
                continue;
            case 6:
                StuReturnBooks06(BookHead, StuHead);
                continue;
            case 7:
                StuState07(BookHead, StuHead);
                continue;
            default:
                Flag = 0;
                continue;
        }
    }
    return 0;
}